package com.my.chapter11;


import java.util.Iterator;
import java.util.Random;

public class practice31 {
    public static void main(String[] args) {
        RandomShapeGenerator rg = new RandomShapeGenerator(10);
        for(Shape s:rg){
            System.out.println(s.getClass().getSimpleName());
        }
    }
}

class RandomShapeGenerator implements Iterable<Shape>{
    Random random = new Random(47);

    private final int quantity;

    public RandomShapeGenerator(int quantity) {
        this.quantity= quantity;
    }

    public Shape nextShape() {
        switch (random.nextInt(3)) {
            default:
            case 0:
                return new Circle();
            case 1:
                return new Square();
            case 2:
                return new Triangle();
        }
    }

    @Override
    public Iterator<Shape> iterator() {
        return new Iterator<Shape>() {
            private int count = 0;

            @Override
            public boolean hasNext() {
                return count < quantity;
            }

            @Override
            public Shape next() {
                ++count;
                return nextShape();
            }

            @Override
            public void remove() {

            }
        };
    }
}

class Shape{
    public void draw(){
        System.out.println("Shape.draw()");
    }
    public void erase(){
        System.out.println("Shape.erase()");
    }
}

class Circle extends Shape {
    @Override
    public void draw(){
        System.out.println("Circle.draw()");
    }
    @Override
    public void erase(){
        System.out.println("Circle.erase()");
    }
}

class Square extends Shape {
    @Override
    public void draw(){
        System.out.println("Square.draw()");
    }
    @Override
    public void erase(){
        System.out.println("Square.erase()");
    }
}

class Triangle extends Shape {
    @Override
    public void draw(){
        System.out.println("Triangle.draw()");
    }
    @Override
    public void erase(){
        System.out.println("Triangle.erase()");
    }
}
